﻿@using System.ComponentModel;
@inject IStringLocalizer<PdfReaderAdvanced> Localizer

<div>
    <div class="row mb-3">
        <div class="col-12">
            <BootstrapInputGroup>
                <Button Color="Color.Primary" OnClick="ApplyZoom">@Localizer["PdfReaderAdvancedViewMode"]</Button>
                <Button Color="Color.Primary" OnClick="ApplyPagemode">@Localizer["PdfReaderAdvancedPageMode"]</Button>
                <Button Color="Color.Primary" OnClick="ApplyPagePrevious">@Localizer["PdfReaderAdvancedPagePrevious"]</Button>
                <Button Color="Color.Primary" OnClick="ApplyPageNext">@Localizer["PdfReaderAdvancedPageNext"]</Button>
                <BootstrapInput @bind-Value="@Page" />
                <Button OnClick="ApplyPage">@Localizer["PdfReaderAdvancedGoToPage"]</Button>
            </BootstrapInputGroup>
        </div>
    </div>
    <div class="row mb-3">
        <div class="col-12">
            <BootstrapInputGroup>
                <BootstrapInputGroupLabel @bind-Value="@Search" DisplayText="@Localizer["PdfReaderAdvancedSearchLabel"]" />
                <BootstrapInput @bind-Value="@Search" />
                <Button Icon="fa-solid fa-magnifying-glass" OnClick="ApplySearch"></Button>
            </BootstrapInputGroup>
        </div>
    </div>
    <div class="row mb-3">
        <div class="col-12">
            <BootstrapInputGroup>
                <BootstrapInputGroupLabel @bind-Value="@Filename" DisplayText="@Localizer["PdfReaderAdvancedFilenameLabel"]" />
                <BootstrapInput @bind-Value="@Filename" />
                <Button Color="Color.Primary" OnClick="ApplySearch">@Localizer["PdfReaderAdvancedRefreshPage"]</Button>
            </BootstrapInputGroup>
        </div>
    </div>
    <div class="row mb-3 form-inline">
        <div class="col-6 col-sm-4 col-md-3 col-lg-auto">
            <Switch @bind-Value="@StreamMode" ShowLabel="true" DisplayText="@Localizer["PdfReaderAdvancedStreamModeLabel"]"/>
        </div>
        <div class="col-6 col-sm-4 col-md-3 col-lg-auto">
            <Switch @bind-Value="@ReadOnly" ShowLabel="true" DisplayText="@Localizer["PdfReaderAdvancedReadOnlyLabel"]" ShowLabelTooltip="true" OnValueChanged="_=>ApplySearch()" />
        </div>
        <BootstrapInput @bind-Value="@Watermark" DisplayText="@Localizer["PdfReaderAdvancedWatermarkLabel"]" />
    </div>

    <PdfReader @ref="PdfReader"
               FileName="@(StreamMode?FilenameStream:Filename)"
               StreamMode="StreamMode"
               Page=@Page
               Zoom=@Zoom
               Pagemode=@Pagemode
               Search=@Search
               Width="80%"
               Height="500px" />
</div>

@code {
    [NotNull]
    PdfReader? PdfReader { get; set; }

    [DisplayName("流模式")]
    private bool StreamMode { get; set; }

    [DisplayName("禁用复制/打印/下载")]
    public bool ReadOnly { get; set; }

    [DisplayName("水印内容")]
    public string Watermark { get; set; } = "www.blazor.zone";

    private EnumZoomMode Zoom { get; set; } = EnumZoomMode.PageHeight;

    private EnumPageMode Pagemode { get; set; } = EnumPageMode.None;

    [DisplayName("文件相对路径或者URL")]
    private string Filename { get; set; } = "/samples/sample.pdf";

    private string FilenameStream { get; set; } = "https://blazor.app1.es/_content/DemoShared/samples/sample2.pdf";

    [DisplayName("搜索")]
    private string? Search { get; set; } = "Performance";

    private int Page { get; set; } = 3;

    private bool ForcePDFJS { get; set; } = true;

    private async Task ApplyZoom()
    {
        Zoom = Zoom switch
        {
            EnumZoomMode.Auto => EnumZoomMode.PageActual,
            EnumZoomMode.PageActual => EnumZoomMode.PageFit,
            EnumZoomMode.PageFit => EnumZoomMode.PageWidth,
            EnumZoomMode.PageWidth => EnumZoomMode.PageHeight,
            EnumZoomMode.PageHeight => EnumZoomMode.Zoom75,
            EnumZoomMode.Zoom75 => EnumZoomMode.Zoom50,
            EnumZoomMode.Zoom50 => EnumZoomMode.Zoom25,
            EnumZoomMode.Zoom25 => EnumZoomMode.Zoom200,
            _ => EnumZoomMode.Auto
        };
        await Refresh();
    }

    private async Task ApplyPagemode()
    {
        Pagemode = Pagemode switch
        {
            EnumPageMode.Thumbs => EnumPageMode.Outline,
            EnumPageMode.Outline => EnumPageMode.Attachments,
            EnumPageMode.Attachments => EnumPageMode.Layers,
            EnumPageMode.Layers => EnumPageMode.None,
            _ => EnumPageMode.Thumbs
        };
        await Refresh();
    }

    async Task Refresh()
    {
        if (PdfReader != null)
            await PdfReader.Refresh(Search, Page, Pagemode, Zoom, ReadOnly, Watermark);
    }

    private async Task ApplyPage()
    {
        Search = null;
        await Refresh();
    }

    private async Task ApplyPagePrevious()
    {
        Page--;
        Search = null;
        await Refresh();
    }

    private async Task ApplyPageNext()
    {
        Page++;
        Search = null;
        await Refresh();
    }

    private async Task ApplySearch() => await Refresh();
}
